ebayseller = {}

ebayseller.listCat;

ebayseller.list = function(){
    main.init();
    fly.ajax({
        service:'/ebaySeller/list',
        success: function(result){
            if(result.status){
                $('#content').html(fly.template('http://naima.vn/ebayseller/list.html', {
                    data:result.data
                }));                            
            }
            else{
                popup.msg(result.message);
            }
        }
    });
}

ebayseller.create = function(){
    popup.open('popup-ebayseller-form', 'Thêm menu tin tức mới', fly.template('http://naima.vn/ebayseller/form.html', {
        data:null
    }), [

    {
        title:'Thêm mới', 
        fn:function(){          
            fly.submit({
                id:'ebayseller-form',
                service: '/ebaySeller/create',
                success: function(result){                    
                    popup.msg(result.message);
                    popup.close('popup-ebayseller-form');
                    $('table tr').first().after('');
                    fly.ajax({
                        service:'/ebayseller/list',
                        success: function(rs){
                            if(rs.status){
                                $('#content').html(fly.template('http://naima.vn/ebayseller/list.html', {
                                    data:rs.data
                                })); 
                                popup.close('popup-ebayseller-form');
                            }
                            else{
                                popup.msg(rs.message);
                            }
                        }
                    });
                }
            });
        }
    },
    {
        title:'Hủy bỏ',
        fn:function(){
            popup.close('popup-ebayseller-form');
        }
    }
    ]);
    fly.editor('ebayseller-description',{
        width:500
    });
    fly.editor('ebayseller-morecontact',{
        width:500
    });
}


ebayseller.edit = function(id){
    fly.ajax({
        service: '/ebaySeller/get',
        loading: false,
        data: {
            id: id
        },
        success: function(data){
            if(!data.status)
                popup.msg(data.message);
            else{
                popup.open('popup-editebayseller-form', 'Chỉnh sửa người bán', fly.template('http://naima.vn/ebayseller/form.html', {
                    data:data.data
                }), [

                {
                    title:'Chỉnh sửa', 
                    fn:function(){            
                        fly.submitWithFile({
                            id:'ebayseller-form',
                            service: '/ebayseller/edit/',
                            success: function(result){
                                if(result.status){
                                    popup.msg(result.message);
                                    popup.close('popup-editmenu-form');
                                    $('table tr').first().after('');
                                    fly.ajax({
                                        service:'/ebayseller/list',
                                        success: function(rs){
                                            if(rs.status){
                                                $('#content').html(fly.template('http://naima.vn/ebayseller/list.html', {
                                                    data:rs.data
                                                })); 
                                                popup.close('popup-editebayseller-form');                    
                                            }
                                            else{
                                                popup.msg(rs.message);
                                            }
                                        }
                                    });
                                }
                                else
                                    popup.msg(result.message);
                            }
                        });
                    }
                },
                {
                    title:'Hủy bỏ',
                    fn:function(){
                        popup.close('popup-editebayseller-form');
                    }
                }
                ]);
                fly.editor('ebayseller-description',{
                    width:500
                });
                fly.editor('ebayseller-morecontact',{
                    width:500
                });
            }
        }
    });    
}
ebayseller.checkAll = function(){
    if($('#list-checkall').is(":checked"))
        $('input[type="checkbox"]').attr('checked','checked');    
    else
        $('input[type="checkbox"]').removeAttr('checked');
}
ebayseller.del = function(id){
    popup.confirm("Bạn có chắc chắn muốn xóa người bán này?", function(){
        fly.ajax({
            service: '/ebaySeller/delete/',
            data: {
                id: id
            },
            success: function(result){
                if(result.status){
                    popup.msg(result.message);
                    $('.table tr[for='+id+']').remove();
                }
                else{
                    popup.msg(result.message);    
                }
            }
        });
    });
}
ebayseller.delAll = function(){
    var data = Array();
    var i = 0;
    $('input[type=checkbox][for]').each(function(){
        if($(this).is(':checked')){
            data[i] = parseInt($(this).attr('for'));
            i++;
        }
    });
    if(data.length <= 0){
        popup.msg("Bạn chưa chọn menu nào.");
        return;
    }
    popup.confirm("Bạn có chắc chắn muốn xóa những người bán này?", function(){
        fly.ajax({
            service: '/ebaySeller/deleteall',
            data:{
                ids:JSON.stringify(data)
            },
            success: function(result){
                popup.msg(result.message);
                for(var i=0; i<result.data.length; i++){
                    $('.table tr[for='+result.data[i]+']').remove();
                }
            }
        });
    });
}
ebayseller.changeStatus = function(id){
    fly.ajax({
        service: '/ebaySeller/changeStatus/',
        data: {
            id: id
        },
        success: function(data){
            if(data.status){
                if(data.data == 1)
                    $('.published-'+id).attr('src',fly.assetsUrl + '/images/icon-enable.png');
                else
                    $('.published-'+id).attr('src',fly.assetsUrl + '/images/icon-disable.png');
            }
            else
                popup.msg(data.message);
        }
    });
}